package praktikumid.p09_t;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Kimp extends JFrame {
	
	class Line {
		public Point start;
		public Point end;
		public Color color;
		
		public Line(Point start) {
			this.start = start;
		}
		
		public void move(int x) {
			start.y = start.y - 1;
			end.y = end.y - 1;
			start.x += x;
			end.x += x;
		}
	}

	ArrayList<Point> points = new ArrayList<Point>();
	
	ArrayList<Line> lines = new ArrayList<Line>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//JFrame j = new JFrame();
		// alar.kuusik@eliko.ee
		Kimp k = new Kimp(600, 400);
	}
	
	public Kimp(int width, int height) {
		this.setSize(width, height);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				points.add(e.getPoint());
				Line l = lines.get(lines.size() - 1);
				l.end = e.getPoint();
				if (e.getPoint().x % 3 == 0) {
					l.color = Color.RED;
				} else if (e.getPoint().x % 3 == 1) {
					l.color = Color.BLACK;
				} else {
					l.color = Color.BLUE;
				}
				repaint();
			}
		});
		
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mousePressed(e);
				for (Line l : lines) {
					l.move(10);
				}
				Line l = new Line(e.getPoint());
				lines.add(l);
				
				
			}
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseReleased(e);
				// last line
				Line l = lines.get(lines.size() - 1);
				l.end = e.getPoint();
			}
		});
		
		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				super.keyPressed(e);
				
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					int result = JOptionPane.showConfirmDialog(null, "Are you sure?", "Exit?", JOptionPane.YES_NO_OPTION);
					if (result == JOptionPane.YES_OPTION) {
						System.exit(0);
					}
				}
			}
		});
		
		
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		
		g.drawLine(0, 0, 100, 100);
		
		g.setColor(Color.BLUE);
		/*for (Point p : points) {
			g.drawRect(p.x, p.y, 2, 2);
		}*/
		for (Line l : lines) {
			g.setColor(l.color);
			g.drawLine(l.start.x, l.start.y, l.end.x, l.end.y);
		}
	}
}
